package realtime.app.dwd.db;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import realtime.util.EnvUtil;
import realtime.util.KafkaUtil;


//id user_id course_id course_name ts
public class DwdTradeCartAdd {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = EnvUtil.getSEE(4);
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);
        tableEnv.executeSql(KafkaUtil.getTopicDbDDL("dwd_trade_cart_add_group"));
        Table cartInfo = tableEnv.sqlQuery("select\n" +
                "    `data`['id'] id,\n" +
                "    `data`['user_id'] user_id,\n" +
                "    `data`['course_id'] course_id,\n" +
                "    `data`['course_name'] course_name,\n" +
                "    ts\n" +
                "from\n" +
                "    topic_db\n" +
                "where \n" +
                "    `table`='cart_info' and `type`='insert'");

        tableEnv.createTemporaryView("cart_info",cartInfo);

        tableEnv.executeSql("CREATE TABLE dwd_trade_cart_add (\n" +
                "    id string,\n" +
                "    user_id string,\n" +
                "    course_id string,\n" +
                "    course_name string,\n" +
                "    ts string,\n" +
                "    PRIMARY KEY (id) NOT ENFORCED\n" +
                ") " + KafkaUtil.getUpsertKafkaDDL("dwd_trade_cart_add"));

        tableEnv.executeSql("insert into dwd_trade_cart_add select * from cart_info");
    }
}
